package com.hrsystem.service;

import com.hrsystem.entity.PersonalData;
import com.hrsystem.mapper.PersonalDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

@Service
public class PersonalDataService {

    @Autowired
    private PersonalDataMapper personalDataMapper;

    /**
     * 添加祖宗第一代
     *
     * @param personalData
     * @return
     */
    public PersonalData getIntoancestors(PersonalData personalData) {
        boolean isSuccess = personalDataMapper.getIntoancestors(personalData) > 0;
        System.out.println("插入一条数据，ID是：" + personalData.getId());
        if (isSuccess) {
            return personalData;
        } else {
            return null;
        }
    }

    /**
     * 添加子代
     *
     * @param personalData
     * @return
     */
    public PersonalData intoChildren(PersonalData personalData) {
        boolean isSuccess = personalDataMapper.getIntoChildren(personalData) > 0;
        System.out.println("插入一条数据，ID是：" + personalData.getId());
        if (isSuccess) {
            return personalData;
        } else {
            return null;
        }
    }

    /**
     * 添加配偶
     *
     * @param personalData
     * @return
     */
    public PersonalData intoSpouse(PersonalData personalData) {
        boolean isSuccess = personalDataMapper.getIntoSpouseId(personalData) > 0;
        System.out.println("添加了配偶，新ID是：" + personalData.getId() + ",添加配偶的人id是" + personalData.getSpouseId());
        personalDataMapper.getIntoSouseIdF(personalData);
        if (isSuccess) {

            return personalData;
        } else {
            return null;
        }
    }

    /**
     * 查询子代及配偶数据
     *
     * @param fatherId
     * @param spouseId
     * @return
     */
    public List<PersonalData> getChildrenSpouse(Integer fatherId, Integer spouseId) {
        List<PersonalData> ChildrenSpouse = personalDataMapper.getChildrenSpouse(fatherId, spouseId);
        System.out.println(fatherId + spouseId);
        if (fatherId != null && spouseId != null) {
            System.out.println(fatherId + spouseId);
            return ChildrenSpouse;
        }
        return null;
    }

    /**
     * 查询配偶数据
     *
     * @param id
     * @return
     */
    public PersonalData checkSpouse(Integer id) {
        PersonalData personalData = personalDataMapper.getPersonalDataSpouseID(id);
        System.out.println(id);
        if (personalData != null) {
            System.out.println(personalData);
            return personalData;
        }
        return null;
    }

    /**
     * 查询个人数据
     *
     * @param id
     * @return
     */
    public PersonalData check(Integer id) {
        PersonalData personalData = personalDataMapper.getPersonalDatapID(id);
        System.out.println(id);
        if (personalData != null) {
            System.out.println(personalData);
            return personalData;
        }
        return null;
    }

    /**
     * 更新个人数据(查询族谱时)
     *
     * @param personalData
     * @return
     * @throws Exception
     */
    public PersonalData updateUser(PersonalData personalData) throws Exception {
        if (personalData.getId() == null) {
            // 如果不包含用户ID，那么不知道要修改哪条用户信息，这种情况下不允许修改，并返回修改失败
            return null;
        }
//        String livingState = "已故";
//        if(personalData.getDeathDate() != null){
//            personalData.getLivingState() = livingState;
//        }
        if (personalDataMapper.getUpdatePersonalDatapID(personalData) > 0) {
            // 影响的条数大于0，表示修改成功
            return personalData;
        } else {
            return null;
        }
    }

    /**
     * 更新个人数据
     *
     * @param personalData
     * @return
     * @throws Exception
     */
    public PersonalData getUpdatePersonManage(PersonalData personalData) throws Exception {
        if (personalData.getId() == null) {
            // 如果不包含用户ID，那么不知道要修改哪条用户信息，这种情况下不允许修改，并返回修改失败
            return null;
        }
        if (personalDataMapper.getUpdatePersonManage(personalData) > 0) {
            // 影响的条数大于0，表示修改成功
            return personalData;
        } else {
            return null;
        }
    }

    /**
     * 删除族员(管理族谱时)
     *
     * @param personalData
     * @return
     * @throws Exception
     */
    public PersonalData getDeletePersonManage(PersonalData personalData) throws Exception {
        if (personalData.getId() == null) {
            // 如果不       包含用户ID，那么不知道要删除哪条用户信息，这种情况下不允许删除，并返回删除失败
            return null;
        }
        if (personalDataMapper.getDeletePersonManage(personalData) > 0) {
            // 影响的条数大于0，表示修改成功
            return personalData;
        } else {
            return null;
        }
    }


    /**
     * 获取祖宗
     *
     * @param adminId 管理员ID
     * @return
     */
    public List<PersonalData> getList(Integer adminId) {
        //查到所有父一代
        List<PersonalData> allSuperBoss = personalDataMapper.getAllSuperBoss(adminId);

        return allSuperBoss;
    }

    /**
     * 获取子代
     *
     * @param fatherId 父亲Id
     * @return
     */
    public List<PersonalData> getAllChildren(Integer fatherId) {
        //寻找子代
        List<PersonalData> children = personalDataMapper.getChildren(fatherId);
        //如果没有子代，就结束
        if (children == null) return null;
        return children;
    }

    /**
     * 查询子代
     *
     * @param id
     * @param adminId
     * @return
     */
    public List<PersonalData> checkchildreninfo(Integer id, Integer adminId) {
        Long start = System.currentTimeMillis();
        System.out.println("start Time : " + start);
        List<PersonalData> personalData = personalDataMapper.getAllSuperBoss(adminId);
        List<PersonalData> all = personalDataMapper.getAll();
        Stack<PersonalData> stack = new Stack<>();
        Stack<Integer> index = new Stack<>();
        stack.addAll(personalData);
        for (int i = 0; i < personalData.size(); i++) {
            index.add(i);
        }
        while (!stack.isEmpty()) {
            PersonalData data = stack.pop();
            data.setChildren(new ArrayList<PersonalData>());
            for (int i = index.pop(); i < all.size(); i++) {
                PersonalData temp = all.get(i);
                if (temp.getFatherId() != null && temp.getFatherId() == (int) data.getId()) {
                    data.getChildren().add(temp);
                    stack.push(temp);
                    index.push(i);
                }
            }
        }
        System.out.println("end Time : " + (System.currentTimeMillis() - start));
        return personalData;
    }

}
